package J20241117_Dual_P;

import java.util.*;
/**
 * Created with IntelliJ IDEA.
 * Description: 滑动窗口——找到字符串中所有字母异位词
 * User: 王圆豪
 * Date: 2024-11-17
 * Time: 23:26
 */
public class Solution {
    public static void main(String[] args) {
        System.out.println(findAnagrams("aa", "bb").toString());
    }

    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> result = new ArrayList<>();
        int l = 0,r = 0;
        int[] hash1 = new int[26];
        for(int i = 0; i < p.length(); i++){
            hash1[p.charAt(i) - 'a']++;
        }
        int[] hash2 = new int[26];
        while(r < s.length()){
            int ch = s.charAt(r) - 'a';
            hash2[ch]++;
            if(r - l + 1 > p.length()){
                hash2[s.charAt(l++) - 'a']--;
            }
            if(Arrays.equals(hash1, hash2)){
                result.add(l);
            }
            r++;
        }
        return result;
    }
}
